home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ecstr3.arc / STRNRPT.C < prev    next >
C/C++ Source or Header  |  1987-03-04  |  944b  |  34 lines

  1. /*  File   : strnrpt.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 20 April 1984
  4.     Defines: strnrpt()
  5.  
  6.     strnrpt(dst, n, src, k) "RePeaTs" the string src into dst  k  times,
  7.     but  will  truncate  the  result at n characters if necessary.  E.g.
  8.     strnrpt(dst, 7, "hack ", 2) will move "hack ha" to dst  WITHOUT  the
  9.     closing  NUL.   The  result  is  the number of characters moved, not
  10.     counting the closing NUL.  Equivalent to strrpt-ing into an infinite
  11.     buffer and then strnmov-ing the result.
  12. */
  13.  
  14. #include "strings.h"
  15.  
  16. int strnrpt(dst, n, src, k)
  17.     register char *dst;
  18.     register int n;
  19.     char *src;
  20.     int k;
  21.     {
  22.        char *save;
  23.  
  24.        for (save = dst; --k >= 0; dst--) {
  25.            register char *p;
  26.            for (p = src; ; ) {
  27.                if (--n < 0) return dst-save;
  28.                if (!(*dst++ = *p++)) break;
  29.            }
  30.        }
  31.        return dst-save;
  32.     }
  33.  
  34.